package com.guanlin.notification.notification;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.widget.RemoteViews;

import androidx.annotation.DrawableRes;
import androidx.core.app.NotificationCompat;

/**
 * author : changer
 * date   : 2020/5/13
 * desc   :
 */
public class NotificationApiCompat {

    Builder mBuilder;

    public NotificationApiCompat(Builder builder) {
        this.mBuilder = builder;
    }


    public Notification getNotificationApiCompat() {
        if (mBuilder.mNotification == null){
            throw new RuntimeException("您是否已经build()????");
        }
        return mBuilder.mNotification;
    }

    /**
     * 开启
     * @param service
     * @param id
     */
    public void startForeground(Service service, int id) {
        service.startForeground(id, mBuilder.mNotification);

    }

    /**
     * 停止前台服务
     * @param service
     */
    public void stopForeground(Service service) {
        service.stopForeground(true);
    }

    /**
     * 显示
     * @param id
     */
    public void notify(int id) {
        mBuilder.notificationUtils.getManager().notify(id,  mBuilder.mNotification);
    }

    /**
     * 更新
     * @param id
     * @param title
     * @param text
     */
    public void updateNotification(int id, String title, String text) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (!TextUtils.isEmpty(text)) {
                mBuilder.mBuilder26.setContentText(text);
            }
            if (!TextUtils.isEmpty(title)) {
                mBuilder.mBuilder26.setContentTitle(title);
            }
            mBuilder.mNotification = mBuilder.mBuilder26.build();
        } else {
            if (!TextUtils.isEmpty(text)) {
                mBuilder.mBuilder25.setContentText(text);
            }
            if (!TextUtils.isEmpty(title)) {
                mBuilder.mBuilder25.setContentTitle(title);
            }
            mBuilder.mNotification = mBuilder.mBuilder25.build();
        }
        mBuilder.notificationUtils.getManager().notify(id, mBuilder.mNotification);
    }


    public static final class Builder {

        Context mContext;
        NotificationUtils notificationUtils;
        //渠道id 渠道名称
        String mChannelId;
        String mChannelName;
        //通知渠道
        NotificationChannel mNotificationChannel;

        private Notification.Builder mBuilder26;
        private NotificationCompat.Builder mBuilder25;
        Notification mNotification;

        int mSmallIcon;

        public Builder(Context context, String channelId, String channelName, @DrawableRes int smallIcon) {
            mContext = context;
            notificationUtils = new NotificationUtils(mContext);
            //渠道id 渠道名称
            mChannelId = channelId;
            mChannelName = channelName;
            mSmallIcon = smallIcon;
            //android 8.0 通知渠道问题
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mNotificationChannel = new NotificationChannel(mChannelId
                        , mChannelName
                        , NotificationManager.IMPORTANCE_DEFAULT);
                mBuilder26 = getChannelNotification(mContext, mChannelId);
                mBuilder26.setSmallIcon(mSmallIcon);
            } else {
                mBuilder25 = getNotification_25(mContext);
                mBuilder25.setSmallIcon(mSmallIcon);
            }
        }

        /**
         * 设置高重要性
         *
         * @return
         */
        public Builder setPrority(NotificationPrority prority) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (prority == NotificationPrority.HIGH) {
                    mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
                } else {
                    mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_DEFAULT);
                }
            } else {
                if (prority == NotificationPrority.HIGH) {
                    mBuilder25.setPriority(NotificationCompat.PRIORITY_MAX);
                } else {
                    mBuilder25.setPriority(NotificationCompat.PRIORITY_MIN);
                }


            }
            return this;
        }

        /**
         * 设置闪灯
         */
        public Builder setLightsEnable(boolean isLightsEnable) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mNotificationChannel.enableLights(isLightsEnable);
            } else {
                mBuilder25.setLights(0xffff0000, 300, 100);
            }
            return this;
        }

        /**
         * 设置震动
         */
        public Builder setVibrate(boolean isLightsEnable) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mNotificationChannel.enableVibration(isLightsEnable);
                mNotificationChannel.setVibrationPattern(new long[]{100, 200});
            } else {
                //震动
                mBuilder25.setVibrate(new long[]{100, 200});
            }
            return this;
        }


        /**
         * 设置大图标
         *
         * @param icon
         * @return
         */
        public Builder setLargeIcon(Bitmap icon) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setLargeIcon(icon);
            } else {
                mBuilder25.setLargeIcon(icon);
            }
            return this;
        }

        /**
         * 设置PendingIntent
         *
         * @param intent
         * @return
         */
        public Builder setContentIntent(PendingIntent intent) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setContentIntent(intent);
            } else {
                mBuilder25.setContentIntent(intent);
            }
            return this;
        }

        /**
         * 设置大的view
         */
        public Builder setBigContentView(RemoteViews remoteViews){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setCustomBigContentView(remoteViews);
            } else {
                mBuilder25.setCustomBigContentView(remoteViews);
            }
            return this;
        }

        /**
         * 设置自定义布局
         */
        public Builder setContentView(RemoteViews remoteViews){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setCustomContentView(remoteViews);
            } else {
                mBuilder25.setContent(remoteViews);
            }
            return this;
        }



        /**
         * 票据
         * @param tickerText
         * @return
         */
        public Builder setTicker(CharSequence tickerText) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setTicker(tickerText);
            } else {
                mBuilder25.setTicker(tickerText);
            }
            return this;
        }

        /**
         * 设置通知标题
         *
         * @param title
         * @return
         */
        public Builder setContentTitle(CharSequence title) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setContentTitle(title);
            } else {
                mBuilder25.setContentTitle(title);
            }
            return this;
        }

        /**
         * 设置通知内容
         *
         * @param text
         * @return
         */
        public Builder setContentText(CharSequence text) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setContentText(text);
            } else {
                mBuilder25.setContentText(text);
            }
            return this;
        }

        /**
         * 是否持续存在
         * @param ongoing
         * @return
         */
        public Builder setOngoing(boolean ongoing) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setOngoing(ongoing);
            } else {
                mBuilder25.setOngoing(ongoing);

            }
            return this;
        }

        /**
         * 是否提示一次.true - 如果Notification已经存在状态栏即使在调用notify函数也不会更新
         * @param onlyAlertOnce
         * @return
         */
        public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setOnlyAlertOnce(onlyAlertOnce);
            } else {
                mBuilder25.setOnlyAlertOnce(onlyAlertOnce);
            }
            return this;
        }

        /**
         * @param max
         * @param progress
         * @param indeterminate
         * @return
         */
        public Builder setProgress(int max, int progress, boolean indeterminate) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setProgress(max, progress, indeterminate);
            } else {
                mBuilder25.setProgress(max, progress, indeterminate);
            }
            return this;
        }

        /**
         * @param when
         * @return
         */
        public Builder setWhen(long when) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setWhen(when);
            } else {
                mBuilder25.setWhen(when);
            }
            return this;
        }

        /**
         * @param defaults
         * @return
         */
        public Builder setDefaults(int defaults) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                mBuilder26.setDefaults(defaults);
            } else {
                mBuilder25.setDefaults(defaults);
            }
            return this;
        }

        /**
         * 设置是否可以自动取消
         *
         * @param autoCancel
         * @return
         */
        public Builder setAutoCancel(boolean autoCancel) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mBuilder26.setAutoCancel(autoCancel);
            } else {
                mBuilder25.setAutoCancel(autoCancel);
            }
            return this;
        }

        /**
         * 大于等于Android 8.0 api>=26
         *
         * @return
         */
        @TargetApi(Build.VERSION_CODES.O)
        private Notification.Builder getChannelNotification(Context context, String channelId) {
            return new Notification.Builder(context, channelId);
        }

        /**
         * 小于Android 8.0 api<26
         *
         * @return
         */
        private NotificationCompat.Builder getNotification_25(Context context) {
            return new NotificationCompat.Builder(context);
        }

        public NotificationApiCompat build() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationUtils.getManager().createNotificationChannel(mNotificationChannel);
                mNotification = mBuilder26.build();
            } else {
                mNotification = mBuilder25.build();
            }
            return new NotificationApiCompat(this);
        }


    }

}
